package com.learn.api.gateway.configs;

import com.learn.api.gateway.service.RedisMessageHandler;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author lwt<br>
 * @description <br>
 * @date 2020/3/5
 */
@Configuration
@EnableCaching //开启注解
public class RedisConfig extends CachingConfigurerSupport {


    @Bean("myStringRedisTemplate")
    public RedisTemplate<String, String> getStringRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(factory);

        // 值采用json序列化
        template.setValueSerializer(new StringRedisSerializer());
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());

        // 设置hash key 和value序列化模式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();

        return template;
    }

    /**
     * redis消息监听器容器
     * 可以添加多个监听不同话题的redis监听器，只需要把消息监听器和相应的消息订阅处理器绑定，该消息监听器
     * 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
     * @param connectionFactory
     * @param routeDefinitionChangeListenerAdapter
     * @return
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            @Qualifier("routeDefinitionChangeListenerAdapter") MessageListenerAdapter routeDefinitionChangeListenerAdapter){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);

        //可以添加多个 messageListener
        container.addMessageListener(routeDefinitionChangeListenerAdapter, new PatternTopic(RedisMessageHandler.topic_routeDefChange));
        //container.addMessageListener(workflowCreatedListenerAdapter, new PatternTopic(UPMWorkflowCreatedMessage.TOPIC_NAME));

        return container;
    }


    /**
     * 消息监听器适配器，绑定消息处理器，利用反射技术调用消息处理器的业务方法
     * @param redisReceiver
     * @return
     */
    @Bean("routeDefinitionChangeListenerAdapter")
    MessageListenerAdapter billRouteDefinitionChangeListenerAdapter(RedisMessageHandler redisReceiver) {
        System.out.println("消息适配器进来了:routeDefinitionChangeListenerAdapter");
        return new MessageListenerAdapter(redisReceiver, "handleDefinitionChange");
    }
    /*@Bean("workflowCreatedListenerAdapter")
    MessageListenerAdapter workflowCreatedListenerAdapter(RedisMessageHandler redisReceiver) {
        System.out.println("消息适配器进来了:workflowCreatedListenerAdapter");
        return new MessageListenerAdapter(redisReceiver, "handleWorkflowCreated");
    }*/

}